<?php
class Widget_GoogleCalendar extends WP_Widget
{
  function Widget_GoogleCalendar()
  {
    $this->WP_Widget('gcal-events', 'Google Calendar Events List', array(
        'classname'   => 'google-calendar-events-list',
        'description' => 'Google Calendar Events List',
      ), array(
        'width'   => 300,
        'height'  => 350,
        'id_base' => 'gcal-events'
      ));
  }
  
  function widget($args, $instance)
  {
    extract($args);
    
    $ical = new Widget_GoogleCalendar_Calendar($instance['url']);
    
    $today    = Date::getInstance()->clearTime();
    $dateFrom = $today->copy()->addDay(-1 * (int) $instance['days_before']);
    $dateTo   = $today->copy()->addDay($instance['days_after']);
    
    $ical->maxResults($instance['max_results']);
    
    $ical->minDate($dateFrom->format('Y-m-d'));
    $ical->maxDate($dateTo->format('Y-m-d'));
    
    $ical->timeZone($instance['timezone']);
    $ical->orderByStartTime();
    $ical->ascendingOrder();
    
    $renderer  = new Widget_GoogleCalendar_Decorator($ical);
    $variables = array();
    
    $title = apply_filters('widget_title', $instance['title']);
    if (strlen($title))
      $variables['title'] = $before_title.$title.$after_title; //WP
    
    $variables['dateformat'] = $instance['dateformat'];
    $variables['from']       = $dateFrom->format($instance['dateformat']);
    $variables['to']         = $dateTo->format($instance['dateformat']);
    
    echo $before_widget; //WP
    $renderer->render($variables);
    echo $after_widget; //WP
  }
  
  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    
    $instance['url']   = $new_instance['url'];
    $instance['title'] = strip_tags($new_instance['title']);
    
    if (!strlen($new_instance['days_before']))
      $new_instance['days_before'] = 5;
    $instance['days_before'] = (int) $new_instance['days_before'];
    
    if (!strlen($new_instance['days_after']))
      $new_instance['days_after'] = 15;
    $instance['days_after']  = abs((int) $new_instance['days_after']);
    
    if (!strlen($new_instance['max_results']))
      $new_instance['max_results'] = 10;
    $instance['max_results'] = abs((int) $new_instance['max_results']);
    
    if (!strlen($new_instance['timezone']))
      $new_instance['timezone'] = 'Europe/Madrid';
    $instance['timezone']    = $new_instance['timezone'];
    
    if (!strlen($new_instance['dateformat']))
      $new_instance['dateformat'] = 'd-m-Y';
    $instance['dateformat']  = $new_instance['dateformat'];
    
    return $instance;
  }
  
  function form($instance)
  {
    $defaults = array(
      'url'   => null,
      'title' => null,
      'days_before' => 5,
      'days_after'  => 15,
      'max_results' => 10,
      'timezone'    => 'Europe/Madrid',
      'dateformat'  => 'd-m-Y',
    );
    $instance = wp_parse_args((array) $instance, $defaults);
  ?>
  
  <p>
    <?php echo label_tag($this->get_field_id('title'), 'Title') ?>
    <?php echo input_tag($this->get_field_name('title'), $instance['title'], array('id' => $this->get_field_id('title'), 'style' => 'width:100%;')) ?>
  </p>
  
  <p>
    <?php echo label_tag($this->get_field_id('url'), 'Calendar URL') ?>
    <?php echo input_tag($this->get_field_name('url'), $instance['url'], array('id' => $this->get_field_id('url'), 'style' => 'width:100%;')) ?>
  </p>
  
  <p>
    <?php echo label_tag($this->get_field_id('days_before'), 'Show days before:') ?>
    <?php echo input_tag($this->get_field_name('days_before'), $instance['days_before'], array('id' => $this->get_field_id('days_before'), 'size' => '2')) ?>
  </p>
  
  <p>
    <?php echo label_tag($this->get_field_id('days_after'), 'Show days after:') ?>
    <?php echo input_tag($this->get_field_name('days_after'), $instance['days_after'], array('id' => $this->get_field_id('days_after'), 'size' => '2')) ?>
  </p>
  
  <p>
    <?php echo label_tag($this->get_field_id('max_results'), 'Max results:') ?>
    <?php echo input_tag($this->get_field_name('max_results'), $instance['max_results'], array('id' => $this->get_field_id('max_results'), 'size' => '2')) ?>
  </p>
  
  <p>
    <?php echo label_tag($this->get_field_id('dateformat'), 'Date format:') ?>
    <?php echo input_tag($this->get_field_name('dateformat'), $instance['dateformat'], array('id' => $this->get_field_id('dateformat'), 'size' => '10;')) ?>
  </p>
  
  <p>
    <?php echo label_tag($this->get_field_id('timezone'), 'Time zone:') ?>
    <?php echo select_tag($this->get_field_name('timezone'), options_for_select(cms_get_datetimezones(), $instance['timezone']), array('id' => $this->get_field_id('timezone'))) ?>
  </p>
  
  <?php
  }
}
